<?php

Class operacionesBD extends PDO {
        private static $instance = array();
        private $id_conect;
        private $host;
        private $usuario;
        private $pass;
        private $bd;

        public function  __construct($host = 'localhost', $usuario = 'root', $pass = '', $bd = 'musica') {
            $this->host = $host;
            $this->usuario = $usuario;
            $this->pass = $pass;
            $this->bd = $bd;
        }

        public function getInstance($host, $usuario, $pass, $bd){
                if(!array_key_exists($host.$usuario.$pass.$bd, self::$instance)){
                        self::$instance[$host.$usuario.$pass.$bd] = new operacionesBD($host, $usuario, $pass, $bd);
                }
                return self::$instance[$host.$usuario.$pass.$bd];
        }

        private function conectar(){
            $dsn = "mysql:dbname={$this->bd};host={$this->host}";
            $user = $this->usuario;
            $password = $this->pass;
            $this->id_connect = new PDO($dsn, $user, $password);
        }

        public function consultar($query){
                $this->conectar();
                $vector = array();
                $i = 0;
                $result = $this->id_connect->query($query);
                if($result){
                    foreach($result as $fila){
                            $vector[$i] = $fila;
                            ++$i;
                    }
                }
                else $vector = array();
                return $vector;
        //      $this->desconectar();
        }

        public function modificar ($query){
                $this->conectar();
                $this->id_connect->query($query);

		return $this->id_connect->lastInsertId();

        //	if(!$id_query)  throw new Exception("Error de query");
        //      $this->desconectar();
        }

        private function desconectar(){
                if (mysql_close($this->id_conect) == 0) throw new Exception("Error al desconectar");
        }
}

?>
